package com.hankai.controller.portal;

import com.hankai.common.Const;
import com.hankai.common.ResponseCode;
import com.hankai.common.ServerResponse;
import com.hankai.pojo.Shipping;
import com.hankai.pojo.User;
import com.hankai.service.IShippingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by hankai on 2017/6/9.
 */
@Controller
@RequestMapping("/shipping/")
public class ShippingController {
    @Resource
    private IShippingService iShippingService;

    /**
     * 添加地址
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping(value = "addShipping.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addShipping(HttpSession session, Shipping shipping){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.addShipping(user.getId(),shipping);
    }

    /**
     * 删除地址
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping(value = "deleteShipping.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteShipping(HttpSession session, Integer shippingId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.deleteShipping(user.getId(),shippingId);
    }

    /**
     * 修改地址
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping(value = "updateShipping.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateShipping(HttpSession session, Shipping shipping){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.updateShipping(user.getId(),shipping);
    }

    /**
     * 查询单个地址
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping(value = "searchShipping.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse searchShipping(HttpSession session, Integer shippingId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.searchShipping(user.getId(),shippingId);
    }

    /**
     * 查询所有地址列表
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "searchShippingAll.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse searchShippingAll(HttpSession session,Integer pageNum,Integer pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.searchShippingAll(user.getId(),pageNum,pageSize);
    }
}
